<template>
  <div>
    <van-nav-bar title="旧密码修改"
                 left-text=""
                 left-arrow
                 @click-left="onClickLeft"/>
    <div class="vanTabs">

      <div class="vanTabsCon">
        <van-cell-group>
          <van-field v-model="oldPsd"
                     clearable
                     label="原登录密码："
                     placeholder="请输入原登录密码"/>

          <van-field v-model="newPsd"
                     type="password"
                     clearable
                     label="新登录密码："
                     placeholder="请输入新登录密码"/>

          <van-field v-model="confirmPsd"
                     type="password"
                     clearablex
                     label="确认新密码："
                     placeholder="请重新新输入密码"/>
        </van-cell-group>
      </div>
    </div>
    <van-button
      class="setPsdBtn"
      type="danger"
      size="large"
      @click="setPsdBtn">确认修改
    </van-button>
  </div>
</template>

<script>
  import {getStore, setStore} from '../../../src/config/mUtils'

  export default {
    name: "setPsd",
    data() {
      return {
        oldPsd: '', //修改-旧密码
        newPsd: '', //修改-新密码
        confirmPsd: '', //修改-确认密码
      }
    },
    methods: {
      //旧密码修改
      setPsdBtn() {
        if ( this.oldPsd.length == '' || this.newPsd.length == '' || this.confirmPsd.length == '' ) {
          this.$toast.fail('信息不可为空')
          return
        }
        if ( this.oldPsd.length < 6 || this.newPsd.length < 6 || this.confirmPsd.length < 6 ) {
          this.$toast.fail('密码最小程度为6位最大长度为16位')
          return
        }

        if ( this.newPsd.trim() !== this.confirmPsd.trim() ) {
          this.$toast.fail('密码两次输入值不一致！')
          return
        }

        this.$axios.post('user/updatepasswd', this.qs.stringify({
          "newpasswd": this.newPsd,
          "passwd": this.oldPsd,
          "token": getStore("token"),
        })).then((res) => {
          if ( res.data.status === 1 ) {
            this.$toast.success(res.data.message)
            setStore("userPsd", this.newPsd)
            this.$router.push({
              path: '/login'
            })
          } else {
            this.$toast.fail(res.data.message)
            return
          }
        });
      },
      onClickLeft() {
        this.$router.go(-1)
      }
    }
  }
</script>

<style scoped>

  .vanTabsCon {
    margin-top: 10px;
  }

  .setPsdBtn {
    width: 95%;
    position: absolute;
    bottom: 0px;
    margin-left: 20px;
  }
</style>
